package com.tnovoselec.android.travelassistant.dto;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;



public class MeteoResultHolder {

	private static List<MeteoFavoritesDto> RESULT = new ArrayList<MeteoFavoritesDto>();

	private MeteoResultHolder() {
		super();
	}

	public synchronized static void init(List<MeteoFavoritesDto> items) {
		MeteoResultHolder.RESULT.clear();
		if (items != null) {
			Iterator<MeteoFavoritesDto> it = items.iterator();
			while(it.hasNext()){
				MeteoFavoritesDto m = it.next();
				if (m.getForecastList()==null || m.getForecastList().size()==0){
					it.remove();
				}
			}
			MeteoResultHolder.RESULT.addAll(items);
		}
	}

	public static void clear() {
		MeteoResultHolder.RESULT.clear();
	}

	public static MeteoFavoritesDto get(int index) {
		return MeteoResultHolder.RESULT.get(index);
	}

	public static List<MeteoFavoritesDto> getAll() {
		// TODO vratiti novu referencu?
		return RESULT;
		// return new ArrayList<MeteoFavoritesDto>(WeatherResultHolder.RESULT);
	}

	public static int size() {
		return MeteoResultHolder.RESULT.size();
	}

	public static int getVisibleItemsCount() {
		int count = 0;
		for (MeteoFavoritesDto mdf : RESULT) {
			if (mdf.isVisible()) {
				count++;
			}
		}
		return count;
	}

	public static MeteoFavoritesDto getItemByPosition(int position) {
		for (MeteoFavoritesDto item : RESULT) {
			if (item.getPosition() == position) {
				return item;
			}
		}
		return null;
	}

	public static List<MeteoFavoritesDto> getAllVisible() {
		List<MeteoFavoritesDto> visibleItems = new ArrayList<MeteoFavoritesDto>();
		for (MeteoFavoritesDto m : RESULT) {
			if (m.isVisible()) {
				visibleItems.add(m);
			}
		}
		return visibleItems;
	}

	public static MeteoFavoritesDto get(String cityId) {
		for (MeteoFavoritesDto meteo : RESULT) {
			if (meteo.getCityId().equals(cityId)) {
				return meteo;
			}
		}
		return null;
	}

}
